package com.zhenwei.zscm.repo;

import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhenwei.zscm.common.base.MybatisWrapper;
import com.zhenwei.zscm.common.utils.Linq;
import com.zhenwei.zscm.entity.ShippingEntity;
import com.zhenwei.zscm.mapper.IShippingMapper;
import com.zhenwei.zscm.mappings.ShippingMapping;
import com.zhenwei.zscm.model.dto.shipping.ShippingDTO;
import com.zhenwei.zscm.model.vo.shipping.ShippingVO;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
* @author DuckyYang
* @description 针对表【shipping】的数据库操作Service实现
* @createDate 2025-05-27 15:27:57
*/
@Service
public class ShippingRepo extends ServiceImpl<IShippingMapper, ShippingEntity> {
    /**
     * 新建一条发货记录
     * @param vo vo
     * @return bool
     */
    public boolean create(ShippingVO vo) {
        vo.setGuid(IdUtil.simpleUUID());
        if(StrUtil.isBlank(vo.getPoNumber())) {
            vo.setPoNumber("");
        }
        var e = ShippingMapping.INSTANCE.vo2Entity(vo);
        return this.save(e);
    }

    /**
     * 检查是否存在相同的订单编号
     * @param orderNumber 订单编号
     * @return bool
     */
    public boolean checkExists(String orderNumber) {
        var q = new LambdaQueryWrapper<ShippingEntity>()
                .eq(ShippingEntity::getOrderNumber, orderNumber)
                .eq(ShippingEntity::getDeleted, false);
        return this.exists(q);
    }
    /**
     * 获取发货订单详情
     * @param orderId 订单id
     * @return ShippingDTO
     */
    public ShippingDTO get(String orderId) {
        var q = new LambdaQueryWrapper<ShippingEntity>()
                .eq(ShippingEntity::getGuid, orderId)
                .eq(ShippingEntity::getDeleted, false);
        List<ShippingEntity> list = this.list(q);
        return Linq.getFirst(list, ShippingMapping.INSTANCE::entity2Dto);
    }

    /**
     * 更新数据，除了系统订单号外都能修改
     * @param vo vo
     * @return bool
     */
    public boolean update(ShippingVO vo) {
        var u = new LambdaUpdateWrapper<ShippingEntity>()
                .eq(ShippingEntity::getGuid, vo.getGuid())
                .eq(ShippingEntity::getDeleted, false)
                .set(ShippingEntity::getPoNumber, StrUtil.isBlank(vo.getPoNumber()) ? "" : vo.getPoNumber())
                .set(ShippingEntity::getPurchaseCompanyCode, vo.getPurchaseCompanyCode())
                .set(ShippingEntity::getPurchaseCompany, vo.getPurchaseCompany())
                .set(ShippingEntity::getContactor, vo.getContactor())
                .set(ShippingEntity::getTelephone, vo.getTelephone())
                .set(ShippingEntity::getAddress, vo.getAddress())
                .set(ShippingEntity::getLogisticsCompany, vo.getLogisticsCompany())
                .set(ShippingEntity::getLogisticsNumber, vo.getLogisticsNumber())
                .set(ShippingEntity::getLogisticsCompanyCode, vo.getLogisticsCompanyCode())
                .set(ShippingEntity::getShippingDate, vo.getShippingDate());
        MybatisWrapper.fillUser(u);
        return this.update(u);
    }

    /**
     * 删除发货订单
     * @param orderId 订单id
     * @return bool
     */
    public boolean delete(String orderId) {
        var u = new LambdaUpdateWrapper<ShippingEntity>()
                .eq(ShippingEntity::getGuid, orderId)
                .eq(ShippingEntity::getDeleted, false)
                .set(ShippingEntity::getDeleted, true);
        MybatisWrapper.fillUser(u);
        return this.update(u);
    }


    @Transactional(rollbackFor = Exception.class)
    public boolean saveAll(List<ShippingDTO> shippingList) {
        List<ShippingEntity> entityList = Linq.mapList(shippingList, ShippingMapping.INSTANCE::dto2Entity);
        return this.saveBatch(entityList, 1000);
    }

    public List<ShippingDTO> listByIds(List<String> ids) {
        var q = new LambdaQueryWrapper<ShippingEntity>()
                .in(ShippingEntity::getGuid, ids)
                .eq(ShippingEntity::getDeleted, false)
                .orderByDesc(ShippingEntity::getId);
        List<ShippingEntity> list = this.list(q);
        return Linq.mapList(list, ShippingMapping.INSTANCE::entity2Dto);
    }

    public ShippingDTO getRecentlyContactor(String purchaseCompanyCode) {
        var q = new LambdaQueryWrapper<ShippingEntity>()
                .eq(ShippingEntity::getPurchaseCompanyCode, purchaseCompanyCode)
                .eq(ShippingEntity::getDeleted, false);
        List<ShippingEntity> list = this.list(q);
        if(Linq.isEmpty(list)) {
            return null;
        }
        return Linq.getFirst(list, ShippingMapping.INSTANCE::entity2Dto);
    }
}




